programming4us
           
 
 
Programming

ASP.NET Security : The Membership and Role Management API (part 3) - Role

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
10/17/2010 6:22:28 PM

Managing Roles

Roles in ASP.NET simplify the implementation of applications that require authorization. A role is just a logical attribute assigned to a user. An ASP.NET role is a plain string that refers to the logical role the user plays in the context of the application. In terms of configuration, each user can be assigned one or more roles. This information is attached to the identity object, and the application code can check it before the execution of critical operations.

For example, an application might define two roles—Admin and Guest, each representative of a set of application-specific permissions. Users belonging to the Admin role can perform tasks that other users are prohibited from performing.

Note

Assigning roles to a user account doesn’t add any security restrictions by itself. It is the responsibility of the application to ensure that authorized users perform critical operations only if they are members of a certain role.


In ASP.NET, the role manager feature simply maintains the relationship between users and roles. ASP.NET 1.1 has no built-in support for managing roles. You can attach some role information to an identity, but this involves writing some custom code. Checking roles is easier, but ASP.NET 2.0 makes the whole thing significantly simpler.

Note

The Role Management API, although it consists of different methods and properties, works like the Membership API from a mechanical standpoint. Many of the concepts you read in the previous section also apply to role management.


The Role Management API

The role management API lets you define roles as well as specify programmatically which users are in which roles. The easiest way to configure role management, define roles, add users to roles, and create access rules is to use WSAT. (See Figure 17-11.) You enable role management by adding the following script to your application’s web.config file:

<roleManager enabled="true" />

You can use roles to establish access rules for pages and folders. The following <authorization> block states that only Admin members can access all the pages controlled by the web.config file:

<configuration>
<system.web>
<authorization>
<allow roles="Admin" />
<deny users="*" />
</authorization>
</system.web>
<configuration>

The order in which you place <allow> and <deny>tags is important. Permissions and denies are processed in the order in which they appear in the configuration file.

WSAT provides a visual interface for creating associations between users and roles. If necessary, you can instead perform this task programmatically by calling various role manager methods. The following code snippet demonstrates how to create the Admin and Guest roles and populate them with usernames:

Roles.CreateRole("Admin");
Roles.AddUsersToRole("DinoE", "Admin");
Roles.CreateRole("Guest");
string[] guests = new string[2];
guests[0] = "JoeUsers";
guests[1] = "Godzilla";
Roles.AddUsersToRole(guests, "Guest")

At run time, information about the logged-in user is available through the HTTP context User object. The following code demonstrates how to determine whether the current user is in a certain role and subsequently enable specific functions:

if (User.IsInRole("Admin"))
{
// Enable functions specific to the role
...
}

When role management is enabled, ASP.NET 2.0 looks up the roles for the current user and binds that information to the User object. This same feature had to be manually coded in ASP.NET 1.x.

Note

In ASP.NET 1.x, you typically cache role information on a per-user basis through a cookie or for all users in a custom Cache entry. In both cases, you do this when the application starts by handling the Application_Start event in the global.asax file. After that, you write a get function to read role information from the store and call it wherever required.


The Roles Class

When role management is enabled, ASP.NET creates an instance of the Roles class and adds it to the current request context—the HttpContext object.

Table 4. Methods of the Roles Class
MethodDescription
AddUsersToRoleAdds an array of users to a role.
AddUsersToRolesAdds an array of users to multiple roles.
AddUserToRoleAdds a user to a role.
AddUserToRolesAdds a user to multiple roles.
CreateRoleCreates a new role.
DeleteCookieDeletes the cookie that the role manager used to cache all the role data.
DeleteRoleDeletes an existing role.
FindUsersInRoleRetrieves all the user names in the specified role that match the provider user name string. The user names found are returned as a string array.
GetAllRolesReturns all the available roles.
GetRolesForUserReturns a string array listing the roles that a particular member belongs to.
GetUsersInRoleReturns a string array listing the users that belong to a particular role.
IsUserInRoleDetermines whether the specified user is in a particular role.
RemoveUserFromRoleRemoves a user from a role.
RemoveUserFromRolesRemoves a user from multiple roles.
RemoveUsersFromRoleRemoves multiple users from a role.
RemoveUsersFromRolesRemoves multiple users from multiple roles.
RoleExistsReturns true if the specified role exists.

Table 5 lists the properties available in the Roles class. All the properties are static and read-only. They owe their value to the settings in the <roleManager> configuration section.

Table 5. Properties of the Roles Class
PropertyDescription
ApplicationNameReturns the provider’s nickname.
CacheRolesInCookieReturns true if cookie storage for role data is enabled.
CookieNameSpecifies the name of the cookie used by the role manager to store the roles. Defaults to .ASPXROLES.
CookiePathSpecifies the cookie path.
CookieProtectionValueSpecifies an option for securing the roles cookie. Possible values are All, Clear, Hashed, and Encrypted.
CookieRequireSSLIndicates whether the cookie requires SSL.
CookieSlidingExpirationIndicates whether the cookie has a fixed expiration time or a sliding expiration.
CookieTimeoutReturns the time, in minutes, after which the cookie will expire.
CreatePersistentCookieCreates a role cookie that survives the current session.
DomainIndicates the domain of the role cookie.
EnabledIndicates whether role management is enabled.
MaxCachedResultsIndicates the maximum number of roles that can be stored in a cookie for a user.
ProviderReturns the current role provider.
ProvidersReturns a list of all supported role providers.

Some methods in the Roles class need to query continuously for the roles associated with a given user, so when possible, the roles for a given user are stored in an encrypted cookie. On each request, ASP.NET checks to see whether the cookie is present; if so, it decrypts the role ticket and attaches any role information to the User object. By default, the cookie is a session cookie and expires as soon as the user closes the browser.

Note that the cookie is valid only if the request is for the current user. When you request role information for other users, the information is read from the data store using the configured role provider.

Note

Role management passes through the role manager HTTP module. The module is responsible for adding the appropriate roles to the current identity object, such as the User object. The module listens for the AuthenticateRequest event and does its job. This is exactly the kind of work you need to code for yourself in ASP.NET 1.x.


The Role Provider

For its I/O activity, the role manager uses the provider model and a provider component. The role provider is a class that inherits the RoleProvider class. The schema of a role provider is not much different from that of a membership provider. Table 6 details the members of the RoleProvider class.

Table 6. Methods of the RoleProvider Class
MethodDescription
AddUsersToRolesAdds an array of users to multiple roles.
CreateRoleCreates a new role.
DeleteRoleDeletes the specified role.
FindUsersInRoleReturns the name of users in a role matching a given user name pattern.
GetAllRolesReturns the list of all available roles.
GetRolesForUserGets all the roles a user belongs to.
GetUsersInRoleGets all the users who participate in the given role.
IsUserInRoleIndicates whether the user belongs to the role.
RemoveUsersFromRolesRemoves an array of users from multiple roles.
RoleExistsIndicates whether a given role exists.

You can see the similarity between some of these methods and the programming interface of the Roles class. As we’ve seen for membership, this is not just coincidental.

ASP.NET ships with a few built-in role providers—SqlRoleProvider (default), WindowsTokenRoleProvider, and AuthorizationStoreRoleProvider. The SqlStoreProvider class stores role information in the same MDF file in SQL Server 2005 Express as the default membership provider. For WindowsTokenRoleProvider, role information is obtained based on the settings defined for the Windows domain (or Active Directory) the user is authenticating against. This provider does not allow for adding or removing roles. The AuthorizationStoreRoleProvider class manages storage of role information for an authorization manager (AzMan) policy store. Supported on Windows Server 2003, Windows XP Professional, and Windows 2000 Server, AzMan is a separate Windows download that enables you to group individual operations together to form tasks. You can then authorize roles to perform specific tasks, individual operations, or both. AzMan provides an MMC snap-in to manage roles, tasks, operations, and users. Role information is stored in a proper policy store, which can be an XML file, an Active Directory, or an ADAM server.

Note

To learn more about AzMan, check the article at http://msdn2.microsoft.com/en-us/library/ms998336.aspx. You can download AzMan from http://windowsupdate.microsoft.com, as it is part of most service packs.


Custom role providers can be created deriving from RoleProvider and registered using the child <providers> section in the <roleManager> section. Note that the process for doing so is nearly identical to the process you saw for the custom membership provider we explored previously.

Other -----------------
- ASP.NET Security : Security-Related Controls (part 2)
- ASP.NET Security : Security-Related Controls (part 1)
- WCF Security Concepts
- Certificate-Based Encryption
- Encryption Using SSL
- Security Privileges and Services
- Client Credentials
- User-Level Security : Service Credentials
- User-Level Security : Custom Authentication
- User-Level Security : Authorization and Impersonation (part 4) - Impersonation
- User-Level Security : Authorization and Impersonation (part 3) - Security Token Authentication
- User-Level Security : Authorization and Impersonation (part 2) - Claims-Based Authorization
- User-Level Security : Authorization and Impersonation (part 1) - Authorization
- Publisher Certificates
- Using LINQ To SQL
- Service Management API (part 2) - Making API Requests
- Service Management API (part 1)
- Windows Services : A Service Control Shell
- ASP.NET Applications and the Web Server
- Internet Information Services (IIS)
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us